package top.readhub.wopei.manager.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.readhub.wopei.common.constants.Constants;
import top.readhub.wopei.common.constants.DataTypeEnum;
import top.readhub.wopei.common.utils.ZKPathUtil;
import top.readhub.wopei.manager.dal.ISettingConfigDao;
import top.readhub.wopei.manager.dal.ISettingRecordDao;
import top.readhub.wopei.manager.dal.model.SettingConfigPO;
import top.readhub.wopei.manager.dal.model.SettingRecordPO;
import top.readhub.wopei.manager.es.ESClient;
import top.readhub.wopei.manager.zk.ZKClient;

import java.util.Date;

/**
 * Created by daervin on 2021/6/7
 */
@Service
public class SettingConfigServiceImpl implements ISettingConfigService {
    @Autowired
    private ISettingConfigDao settingConfigDao;
    @Autowired
    private ISettingRecordDao settingRecordDao;
    @Autowired
    private ZKClient zkClient;
    @Autowired
    private ESClient esClient;

    @Override
    public Long add(SettingConfigPO op) {
        try {
            Date now = new Date();
            op.setCreateTime(now);
            op.setCreateUser("system");
            op.setUpdateTime(now);
            op.setUpdateUser("system");
            settingConfigDao.insert(op);
            return op.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer del(Long id) {
        return settingConfigDao.delete(id);
    }

    @Override
    public Long addRecord(SettingRecordPO po) {
        SettingConfigPO configPO = settingConfigDao.selectById(po.getConfigId());
        if (configPO == null) {
            return null;
        }
        if (configPO.getDataType() == DataTypeEnum.TEMPLATE.ordinal()) {
            Date now = new Date();
            po.setCreateTime(now);
            po.setCreateUser("system");
            po.setUpdateTime(now);
            po.setUpdateUser("system");
            int count = settingRecordDao.insert(po);
            if (count > 0) {
                resetZKData(configPO, po);
                return po.getId();
            }
            return null;
        }
        //todo 多数据处理
        return null;
    }

    @Override
    public Integer updateRecord(SettingRecordPO po) {

        SettingConfigPO configPO = settingConfigDao.selectById(po.getConfigId());
        if (configPO == null) {
            return null;
        }
        SettingRecordPO recordPO = settingRecordDao.selectById(po.getId());
        if (recordPO == null || recordPO.getConfigId().compareTo(configPO.getId()) != 0) {
            return null;
        }
        int count = settingRecordDao.update(po);
        if (count > 0) {
            resetZKData(configPO, po);
        }
        //todo 多数据处理
        return count;
    }

    @Override
    public Integer delRecord(Long id) {
        return null;
    }

    private void resetZKData(SettingConfigPO configPO, SettingRecordPO recordPO) {
        if (configPO == null || recordPO == null) {
            return;
        }
        if (StringUtils.isEmpty(recordPO.getContent())) {
            return;
        }
        try {
            zkClient.setData(ZKPathUtil.path(configPO.getCode(), Constants.SUB_DEFAULT_PATH), recordPO.getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
